package com.google.gson.internal.bind;

import com.google.gson.b.c;
import com.google.gson.b.d;
import com.google.gson.internal.b;
import com.google.gson.internal.bind.TypeAdapter.Factory;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public final class a extends TypeAdapter {
    public static final Factory a = new Factory() {
        public TypeAdapter create(l lVar, com.google.gson.a.a aVar) {
            Type b = aVar.b();
            if (!(b instanceof GenericArrayType) && (!(b instanceof Class) || !((Class) b).isArray())) {
                return null;
            }
            b = b.g(b);
            return new a(lVar, lVar.a(com.google.gson.a.a.a(b)), b.e(b));
        }
    };
    private final Class b;
    private final TypeAdapter c;

    public a(l lVar, TypeAdapter typeAdapter, Class cls) {
        this.c = new x(lVar, typeAdapter, cls);
        this.b = cls;
    }

    public void a(d dVar, Object obj) throws IOException {
        if (obj == null) {
            dVar.f();
            return;
        }
        dVar.b();
        int length = Array.getLength(obj);
        for (int i = 0; i < length; i++) {
            this.c.a(dVar, Array.get(obj, i));
        }
        dVar.c();
    }

    public Object b(com.google.gson.b.a aVar) throws IOException {
        if (aVar.f() == c.NULL) {
            aVar.j();
            return null;
        }
        List arrayList = new ArrayList();
        aVar.a();
        while (aVar.e()) {
            arrayList.add(this.c.b(aVar));
        }
        aVar.b();
        Object newInstance = Array.newInstance(this.b, arrayList.size());
        for (int i = 0; i < arrayList.size(); i++) {
            Array.set(newInstance, i, arrayList.get(i));
        }
        return newInstance;
    }
}
